<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\AttributeGroup
 *
 * @property int $id
 * @property string $title name of group
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AttributeKey[] $attributeKeys
 * @property-read int|null $attribute_keys_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Product[] $products
 * @property-read int|null $products_count
 * @method static \Database\Factories\AttributeGroupFactory factory(...$parameters)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeGroup newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeGroup newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeGroup query()
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeGroup whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeGroup whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeGroup whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeGroup whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class AttributeGroup extends Model
{
    use HasFactory;
    
    protected $guarded = ['id'];
    
    protected $with = ['attributeKeys'];
	
	public static function tree()
	{
		$groups = [0 => '请选择'];
		foreach (self::all() as $group) {
			$groups[$group->id] = $group->title;
		}
		return collect($groups);
	}
    
    public function attributeKeys()
    {
    	return $this->hasMany(AttributeKey::class);
    }
    
    public function products()
    {
    	return $this->hasMany(Product::class);
    }
}
